Chapter 1

The way of the program

based on How to Think Like a Computer Scientist: Python Version :Chapter 1

The goal of this class is to teach you to think like a computer scientist.

This way of thinking combines some of the best features of:

The single most important skill for a computer scientist is problem solving: Learning to program is about learning to solve problems.

1.1 The Python programming language

Two types of programming languages: Computers only execute low level languages. Programs written in high level languages are translated to the machine language of the specific computer.

Advantage of low-level programming language: program can be tuned to specific computer for maximum execution speed and minimum memory consumption.

Advantages of high-level programming languages:

Today almost all programs are written in high-level programming languages.

Two types of programs translate high-level languages to machine language: interpreters and compilers.

Python is considered an interpreted language because Python programs are executed by an interpreter.

The interpreter can be run in command-line mode or script mode:

1.2 What is a program?

A program is a sequence of instructions that specifies how to perform a computation.

Examples:

Instructions (or commands, or statements) look different in different programming languages, but a few basic instructions appear in almost every language: All programs consist of instructions like these. So one way to describe programming is the process of breaking a large, complex task up into smaller and smaller subtasks until eventually the subtasks are simple enough to be performed with one of these simple instructions.

We will come back to this topic later when we discuss algorithms.

1.3 What is debugging?

Programming errors are called bugs. The process of tracking them down and correcting them is called debugging.

There are three kinds of program errors:

1.3.1 Syntax errors

Python cannot execute a program unless it is syntactically correct. Otherwise it returns an error message without starting the program. Syntax refers to the structure of your program and rules about that structure.

In English, a sentence must begin with a capital letter and end with a period.

A Python example is mismatched parenthesis:
      (5/2))
           ^
  SyntaxError: invalid syntax

1.3.2 Run-time errors

An example is a division-by-zero error:
   >>> 5/0
   Traceback (innermost last):
     File "<pyshell#9>", line 1, in ?
       5/0
   ZeroDivisionError: integer division or modulo

1.3.3 Logic or semantic errors

In this case the program runs successfully, in the sense that it does not generate any error messages, but it doesn't do what you want it to do. Instead it is doing what you told it to do!

Debugging logical errors requires you to work backwards from the observed output (if any) to determine what the program is actually doing internally.

1.4 Experimental Debugging

One of the most important skills you will acquire in this class is debugging.

Some programmers (such as the authors of How to Think Like a Computer Scientist) believe debugging is one of the most intellectually rich, challenging, and interesting parts of programming.

Debugging is like detective work:

Debugging is also like an experimental science:
  1. Form hypothesis about cause of error.
  2. Modify program, and predict new outcome.
  3. If results match prediction, hypothesis is correct.
  4. Otherwise, modify hypothesis.
"When you have eliminated the impossible, whatever remains, however improbable, must be the truth" (Sherlock Holmes, from A. Conan Doyle's The Sign of Four)

For some people, programming and debugging are the same thing:

1.5 Formal and natural languages

Natural Languages Formal Languages are designed for specific applications: Formal languages tend to have strict rules about syntax. Two types of syntax rules: The process of determining the structure of a sentence in a natural or formal language is called parsing. After parsing a sentence you can determine the meaning or semantics of the sentence.

Formal and natural languages share:

Formal and natural languages differ on: The difference between formal and natural languages has been compared to the difference between prose and poetry.

1.6 The first program

Traditionally the first program people write in a new language is called "Hello, World!" because all it does is print the words "Hello, World!" In Python, this program looks like this:
  print "Hello, World!"